/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.controller.pc;

import lineage.model.gameworld.L1World;
import lineage.model.instance.L1PcInstance;
import lineage.packet.server.S_ObjectRemove;

/**
 * 角色离开游戏的相关处理.
 * 
 * @version 2014年3月22日下午8:29:26
 * @author jrwz
 */
public final class QuitGame {
    /**
     * 开始处理.
     * 
     * @param - pc 要处理的角色
     */
    protected static final void execute(final L1PcInstance pc) {
        L1PcController.remove(pc); // 从在线列表中移除
        pc.save(); // 保存资料至数据库
        L1World.getInstance().removeObject(pc); // 移出世界
        L1World.getInstance().removeVisibleObject(pc); // 移出世界(区分地图)

        pc.getKnownPlayers().forEach(knownPc -> {
            if (knownPc.isKnownsPlayer(pc)) {
                knownPc.removeKnownPlayer(pc);
                knownPc.sendPackets(new S_ObjectRemove(pc)); // 删除自己(向所有认识自己的对象发送)
            }
        });
        pc.removeAllKnownObjects(); // 清空‘已认识角色’列表
    }
}
